use Qt for os independent environmental variable access. (#856)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Thu, 24 Feb 2022 13:02:08 +0000 (06:02 -0700)
committerGitHub <noreply@github.com>
Thu, 24 Feb 2022 13:02:08 +0000 (06:02 -0700)
defs.h
gpsbabel-sample.ini
inifile.cc
inifile.h
util.cc

diff --git a/defs.h b/defs.h
index f08c9d0136e26bb9fa839a83647cd21f6f87c961..f924ad4b24774d7c64a070d5d17e42dff8d3fab0 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -1059,9 +1059,6 @@ FILE* xfopen(const char* fname, const char* type, const char* errtxt);
 // Thin wrapper around fopen() that supports Unicode fname on all platforms.
 FILE* ufopen(const QString& fname, const char* mode);
 
-// OS-abstracting wrapper for getting Unicode environment variables.
-QString ugetenv(const char* env_var);
-
 // FIXME: case_ignore_strcmp() and case_ignore_strncmp() should probably
 // just be replaced at the call sites.  These shims are just here to make
 // them more accommodating of QString input.
index 743e9d704294a31cd55e5957d4e99319879de407..d2feb02173a24c18410e85dc2d09761ceff49304 100644 (file)
@@ -24,12 +24,3 @@ Prec = 6
 
 [ gdb ]
 via = 1
-
-;------------------------------------------------------------------
-[ tiger ]
-snlen=7
-
-;------------------------------------------------------------------
-[pathaway]
-# dbname = The Last Trip
-deficon = Golf Course
index 404891cd1b861766196e50b048a885e430ff51e1..cb0279071c94483c64097f030a8676ff9ee91473 100644 (file)
@@ -18,7 +18,7 @@
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 
-#include "defs.h"              // for fatal, ugetenv, warning
+#include "defs.h"              // for fatal, warning
 #include "inifile.h"
 #include "src/core/file.h"     // for File
 #include <QByteArray>          // for QByteArray
@@ -29,8 +29,7 @@
 #include <QHash>               // for QHash
 #include <QIODevice>           // for QIODevice::ReadOnly, QIODevice
 #include <QTextStream>         // for QTextStream
-#include <Qt>                  // for CaseInsensitive
-#include <QtGlobal>            // for qPrintable
+#include <QtGlobal>            // for qEnvironmentVariable, qPrintable, QT_VERSION, QT_VERSION_CHECK
 #include <utility>
 
 #define MYNAME "inifile"
@@ -66,7 +65,7 @@ open_gpsbabel_inifile()
 {
   QString res;
 
-  QString envstr = ugetenv("GPSBABELINI");
+  QString envstr = qEnvironmentVariable("GPSBABELINI");
   if (!envstr.isNull()) {
     if (QFile(envstr).open(QIODevice::ReadOnly)) {
       return envstr;
@@ -79,9 +78,9 @@ open_gpsbabel_inifile()
 #ifdef __WIN32__
     // Use &&'s early-out behaviour to try successive file locations: first
     // %APPDATA%, then %WINDIR%, then %SYSTEMROOT%.
-    (name = find_gpsbabel_inifile(ugetenv("APPDATA"))).isNull()
-    && (name = find_gpsbabel_inifile(ugetenv("WINDIR"))).isNull()
-    && (name = find_gpsbabel_inifile(ugetenv("SYSTEMROOT"))).isNull();
+    (name = find_gpsbabel_inifile(qEnvironmentVariable("APPDATA"))).isNull()
+    && (name = find_gpsbabel_inifile(qEnvironmentVariable("WINDIR"))).isNull()
+    && (name = find_gpsbabel_inifile(qEnvironmentVariable("SYSTEMROOT"))).isNull();
 #else
     // Use &&'s early-out behaviour to try successive file locations: first
     // ~/.gpsbabel, then /usr/local/etc, then /etc.
index e1aa6afd4a3cdf6ffce9df0c92634dd38587527d..ef42c08760ad2e9f89e968df2c07d961dff63276 100644 (file)
--- a/inifile.h
+++ b/inifile.h
@@ -22,7 +22,6 @@
 #define HAVE_INIFILE_H
 
 #include <QHash>           // for QHash
-#include <QList>           // for QList
 #include <QString>         // for QString
 
 class InifileSection;
diff --git a/util.cc b/util.cc
index 0bae87a0e7fb6ced65b636350aa748bebe165739..a329463c7eabee3b8ac99d1372968b178162cfcc 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -26,7 +26,7 @@
 #include <cstdarg>                      // for va_list, va_end, va_start, va_copy
 #include <cstdio>                       // for size_t, vsnprintf, FILE, fopen, printf, sprintf, stderr, stdin, stdout
 #include <cstdint>                      // for uint32_t
-#include <cstdlib>                      // for abs, getenv, calloc, free, malloc, realloc
+#include <cstdlib>                      // for abs, calloc, free, malloc, realloc
 #include <cstring>                      // for strlen, strcat, strstr, memcpy, strcmp, strcpy, strdup, strchr, strerror
 #include <ctime>                        // for mktime, localtime
 
@@ -44,7 +44,7 @@
 #include <QXmlStreamAttributes>         // for QXmlStreamAttributes
 #include <Qt>                           // for CaseInsensitive
 #include <QTimeZone>                    // for QTimeZone
-#include <QtGlobal>                     // for qAsConst, QAddConst<>::Type, qPrintable
+#include <QtGlobal>                     // for qAsConst, qEnvironmentVariableIsSet, QAddConst<>::Type, qPrintable
 
 #include "defs.h"
 #include "src/core/datetime.h"          // for DateTime
@@ -206,21 +206,6 @@ ufopen(const QString& fname, const char* mode)
 #endif
 }
 
-/*
- * OS-abstracting wrapper for getting Unicode environment variables.
- */
-QString ugetenv(const char* env_var)
-{
-#ifdef __WIN32__
-  // Use QString to convert 8-bit env_var argument to wchar_t* for _wgetenv().
-  return QString::fromWCharArray(
-           _wgetenv((const wchar_t*) QString(env_var).utf16()));
-#else
-  // Everyone else uses UTF-8 or some other locale-specific 8-bit encoding.
-  return QString::fromLocal8Bit(std::getenv(env_var));
-#endif
-}
-
 /*
  * Allocate a string using a format list with optional arguments
  * Returns -1 on error.
@@ -721,7 +706,7 @@ mklocaltime(struct tm* t)
 bool
 gpsbabel_testmode()
 {
-  static bool testmode = getenv("GPSBABEL_FREEZE_TIME") != nullptr;
+  static bool testmode = qEnvironmentVariableIsSet("GPSBABEL_FREEZE_TIME");
   return testmode;
 }